package marlon.smsbatcheditor.view;

import marlon.smsbatcheditor.R;
import marlon.smsbatcheditor.model.RuleCondition;
import android.content.Context;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 
 * @author marlonyao<yaolei135@gmail.com>
 */
public class RuleConditionView extends RelativeLayout implements OnItemSelectedListener {
	// event listener class
	public interface OnRemoveListener {
		void onRemove(RuleConditionView view);
	}
	
	// controls
	private Spinner spCondTerm;
	private Spinner spCondPredict;
	private EditText etCondValue;
	private ImageView btnDelete;
	
	// adapters
	private ArrayAdapter<CharSequence> adpContains;
	private ArrayAdapter<CharSequence> adpReads;
	
	// listeners
	private OnRemoveListener onRemoveListener;
	
	public RuleConditionView(RuleCondition model, Context context) {
		super(context);
		_init(model);
	}

	public RuleConditionView(Context context) {
		this(null, context);
	}
	
	private void _init(RuleCondition model) {
		LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li.inflate(R.layout.rule_condition, this, true);
		
		spCondTerm = (Spinner) findViewById(R.id.spCondTerm);
		spCondTerm.setAdapter(createSpinnerAdapter(R.array.array_rule_condition_term));
		spCondTerm.setOnItemSelectedListener(this);
		btnDelete = (ImageView) findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (onRemoveListener != null)
					onRemoveListener.onRemove(RuleConditionView.this);
			}
		});
		
		spCondPredict = (Spinner) findViewById(R.id.spCondPredict);
		adpContains = createSpinnerAdapter(R.array.array_rule_condition_contains);
		adpReads = createSpinnerAdapter(R.array.array_rule_condition_read);
		
		etCondValue = (EditText) findViewById(R.id.etCondValue);
		
		if (model != null) {
			spCondTerm.setSelection(model.getTerm().ordinal());
			
			if (model.getPredict() instanceof RuleCondition.Read) {
				spCondPredict.setAdapter(adpReads);
				RuleCondition.Read read = (RuleCondition.Read) model.getPredict();
				spCondPredict.setSelection(read.ordinal());
			} else {
				spCondPredict.setAdapter(adpContains);
				RuleCondition.Contains contains = (RuleCondition.Contains) model.getPredict();
				spCondPredict.setSelection(contains.ordinal());
				
				etCondValue.setVisibility(VISIBLE);
				etCondValue.setText(model.getValue());
			}
		}
	}
	
	private ArrayAdapter<CharSequence> createSpinnerAdapter(int arrResId) {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), arrResId, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return adapter;
	}
	
	private boolean condValueVisible() {
		return spCondTerm.getSelectedItemPosition() > 0;
	}
	
	private String getCondValue() {
		if (condValueVisible())
			return etCondValue.getText().toString();
		else
			return null;
	}
	
	private void updateView() {
		if (condValueVisible()) { // ALL
			if (spCondPredict.getAdapter() != adpContains)
				spCondPredict.setAdapter(adpContains);
			
			etCondValue.setVisibility(VISIBLE);
			if (spCondTerm.getSelectedItemPosition() == RuleCondition.Term.ADDRESS.ordinal()) {
				etCondValue.setInputType(InputType.TYPE_CLASS_PHONE);
			} else {
				etCondValue.setInputType(InputType.TYPE_CLASS_TEXT);
			}
		} else {
			if (spCondPredict.getAdapter() != adpReads)
				spCondPredict.setAdapter(adpReads);
			etCondValue.setVisibility(GONE);
		}
	}
	
	public RuleCondition getRuleCondition() {
		RuleCondition.Term term = RuleCondition.Term.values()[spCondTerm.getSelectedItemPosition()];
		if (term == RuleCondition.Term.ALL) {
			RuleCondition.Read read = RuleCondition.Read.values()[spCondPredict.getSelectedItemPosition()];
			if (read == RuleCondition.Read.ALL)
				return RuleCondition.all();
			else if (read == RuleCondition.Read.READ)
				return RuleCondition.read();
			else
				return RuleCondition.unread();
		} else {
			return new RuleCondition(term, RuleCondition.Contains.values()[spCondPredict.getSelectedItemPosition()], getCondValue());
		}
	}
	
	public boolean validate() {
		if (condValueVisible() && getCondValue().length() == 0) {
			Toast.makeText(getContext(), "Must specify condition value", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		updateView();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// OnItemSelectedListener interface, do nothing
	}
	
	public void setOnRemoveListener(OnRemoveListener listener) {
		this.onRemoveListener = listener;
	}
}
